package com.sgb.kjwl.viewmodel.shopping;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ShopGoodsInfoEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.fragment.shop.ShopMaterialChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ShopMateriaChildModel extends BaseViewModel {
    private ShopMaterialChildFragment fragment;
    private ShoppingCarRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public ShopMateriaChildModel(ShopMaterialChildFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }
    public void queryListByStore(Map map) {
        page = 1;
        map.put("pageNum",page);
        map.put("pageSize",size);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().queryListByStore(map).enqueue(new Callback<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> call, Response<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    total = response.body().getData().getTotal();
                    if (total > 0 && response.body().getData() != null) {
                        fragment.refresh(response.body().getData().getList(),response.body().getData().isLastPage());
                    }else {
                        fragment.empty();
                    }
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getContext(), t.getMessage());
            }
        });
    }
    public void loadMore(Map map) {
        page = page + 1;
        map.put("pageNum",page);
        map.put("pageSize",size);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().queryListByStore(map).enqueue(new Callback<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> call, Response<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    total = response.body().getData().getTotal();
                    fragment.loadMore(response.body().getData().getList(),response.body().getData().isLastPage());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ShopGoodsInfoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getContext(), t.getMessage());
            }
        });
    }
}
